/**
 * document api
 * @author Cyj
 */

window.cha = {
  create(string) {
    // return document.createElement(tagName)
    const container = document.createElement('template')
    container.innerHTML = string.trim()
    return container.content.firstChild
  },
  // 插入到node后面
  after(node, newNode) {
    node.parentNode.insertBefore(newNode, node.nextSibling)
  },
  // 插入到node前面
  before(node, newNode) {
    node.parentNode.insertBefore(newNode, node)
  },
  append(parent, newNode) {
    parent.appendChild(newNode)
  },
  // 给节点嵌套父节点
  wrap(node, newParent) {
    cha.before(node, newParent)
    cha.append(newParent, node)
  },
  // 删除节点
  remove(node) {
    node.parentNode.removeChild(node)
    return node
  },
  // 删除节点内的节点
  empty(node) {
    const array = []
    let x = node.firstChild
    while (x) {
      let temp = cha.remove(node.firstChild)
      temp.textContent.trim() !== '' && array.push(temp)
      x = node.firstChild
    }
    return array
  },
  // 设置/获取属性
  attr(node, name, value) {
    if (arguments.length === 3) {
      node.setAttribute(name, value)
    } else if (arguments.length === 2) {
      return node.getAttribute(name)
    }
  },
  text(node, string) {
    // 适配
    if (arguments.length === 2) {
      if ('innerText' in node) {
        node.innerText = string
      } else {
        node.textContent = string
      }
    } else if (arguments.length === 1) {
      if ('innerText' in node) {
        return node.innerText
      } else {
        return node.textContent
      }
    }
  },
  html(node, string) {
    if (arguments.length === 2) {
      node.innerHTML = string
    } else if (arguments.length === 1) {
      return node.innerHTML
    }
  },
  style(node, name, value) {
    if (arguments.length === 3) {
      node.style[name] = value
    } else if (arguments.length === 2) {
      if (typeof name === 'string') {
        return node.style[name]
      } else if (typeof name === 'object') {
        for (let key in name) {
          // key:border / color
          node.style[key] = name[key]
        }
      }
    }
  },
  class: {
    add(node, className) {
      node.classList.add(className)
    },
    remove(node, className) {
      node.classList.remove(className)
    },
    has(node, className) {
      return node.classList.contains(className)
    }
  },
  on(node, eventName, fn) {
    node.addEventListener(eventName, fn)
  },
  off(node, evenetName, fn) {
    node.removeEventListener(evenetName, fn)
  },
  find(selector, scope) {
    return (scope || document).querySelectorAll(selector)
  },
  parent(node) {
    return node.parentNode
  },
  children(node) {
    return node.children
  },
  silblings(node) {
    return Array.from(node.parentNode.children).filter(n => n !== node)
  },
  next(node) {
    let x = node.nextSibling
    while (x && x.nodeType === 3) {
      x = x.nextSibling
    }
    return x
  },
  previous(node) {
    let x = node.previousSibling
    while (x && x.nodeType === 3) {
      x = x.previousSibling
    }
    return x
  },
  each(nodeList, fn) {
    for (let i = 0; i < nodeList.length; i++) {
      fn.call(null, nodeList[i])
    }
  },
  index(node) {
    const list = dom.children(node.parentNode)
    let i
    for (i = 0; i < list.length; i++) {
      if (list[i] === node)
        break
    }
    return i
  }
};